﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;

using BlueCMS.Core.Data;
using BlueCMS.Core.Data.Query;
using BlueCMS.Core.Entities;

namespace BlueCMS.Core.Services
{
    public class WebsiteService : ServiceBase
    {
        //public static Website GetWebsiteForDomain(string domainName)
        //{
        //    var tmp = from d in DomainName.Table
        //              join w in Website.Table on d.website_id equals w.id
        //              where d.domain_nm == domainName
        //              select w;
        //    return tmp.FirstOrDefault();
        //}
        public static Website GetWebsiteForDomain(string domainName)
        {
            SelectQuery query = new SelectQuery();
            query.Select
                .Column("w.*");
            query.From
                .Table("cms_website_tbl").As("w")
                .Join("cms_domainname_tbl").As("d").On(c => c.Column("w.website_id").Equals().Column("d.website_id"));
            query.Where
                .Column("d.domain_nm").Equals().Parameter(domainName)
                .And()
                .Column("w.isactive").Equals().Constant(true)
                .And()
                .Column("w.isdeleted").Equals().Constant(false);

            return query.ExecuteObject<Website>();
        }

        public static void DeleteWebsite(Website website)
        {
            website.isdeleted = true;
            SaveWebsite(website);
        }

        public static void SaveWebsite(Website website)
        {
            if (website.website_id == Guid.Empty)
            {
                website.website_id = Guid.NewGuid();
                InsertQuery query = new InsertQuery();
                query.Insert.Table("cms_website_tbl");
                query.Insert
                    .Column("website_id")
                    .Column("isactive");
                query.Values
                    .Parameter(website.website_id)
                    .Parameter(website.isactive);
            }
            else
            {
                UpdateQuery query = new UpdateQuery();
                query.Update.Table("cms_website_tbl");
                query.Set
                    .Column("isactive").Parameter(website.isactive)
                    .Column("blah").Constant(1);
                query.Where.Column("website_id").Equals().Parameter(website.website_id);
            }
        }
    }
}
